package com.signway.javaui.utils;

import com.signway.javaui.ResourceTable;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;

public class SwToast {
    public static final String TAG = SwToast.class.getSimpleName();
    public static final int LONG_TIME = 3000;
    public static final int SHORT_TIME = 2000;

    public static Component initView(Context context, String string) {

        DirectionalLayout directionalLayout = (DirectionalLayout) LayoutScatter
                .getInstance(context)
                .parse(ResourceTable.Layout_component_toast, null, false);

        Text text = (Text) directionalLayout.findComponentById(ResourceTable.Id_toast_text);
        text.setText(string);

        return directionalLayout;
    }

    public static void showToast(Context context, String string) {
        SwLog.info(TAG, "show toast " + string);
        new ToastDialog(context)
                .setComponent(initView(context, string))
                .setAlignment(LayoutAlignment.TOP)
                .setTransparent(true)
                .setOffset(0, SwUtils.toPixels(context, 20))
                .setDuration(LONG_TIME)
                .show();
    }

    public static void showToast(Context context, String string, int durMs) {
        SwLog.info(TAG, "show toast " + string);
        new ToastDialog(context)
                .setComponent(initView(context, string))
                .setAlignment(LayoutAlignment.TOP)
                .setTransparent(true)
                .setOffset(0, SwUtils.toPixels(context, 20))
                .setDuration(durMs)
                .show();
    }
}
